package com._54programer.demo.nio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @author SN
 * @date 2019/11/28
 * @description Nio客户端
 */
public class NioClient {

    public static void main(String[] args) throws Exception{

        //1.创建通道
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        //2.连接服务器
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 10001);
        if(!socketChannel.connect(inetSocketAddress)){
            while (!socketChannel.finishConnect()){
                System.out.println("连接服务器中.....");
            }
        }
        //发送数据
        String str = "hello NioServer";
        ByteBuffer buffer = ByteBuffer.wrap(str.getBytes());
        socketChannel.write(buffer);
        System.in.read();

    }

}
